package com.why.amallrest.controller;

import com.why.common.utils.PageUtils;
import com.why.common.utils.Result;
import com.why.entity.ShoppingCartEntity;
import com.why.service.ShoppingCartService;
import com.why.vo.ShoppingCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: mall-parent
 * @description:
 * @author: @why
 * @create: 2021-10-30 12:38
 **/
@RestController
@RequestMapping("/mall/shopCart")
public class ShopCartController {

    private static final ConcurrentHashMap map = new ConcurrentHashMap();

    @Autowired
    private ShoppingCartService service;

    /**
     * 列表
     */
    @RequestMapping("/list/{userId}")
    public Result list(@PathVariable("userId") Long userId) {
        if (userId == null) {
            return Result.error("userId不能为空!");
        }
        List<ShoppingCartVO> data = service.getShoppingCartVOs(userId);
        return Result.ok().put("page", data);
    }

    @RequestMapping("/list")
    public Result listShopCard(@RequestParam("cids") Set<Long> userIds) {
        if (userIds == null || userIds.size() == 0) {
            return Result.error("userId不能为空!");
        }
        Collection<ShoppingCartEntity> data = service.getListByIds(userIds);
        return Result.ok().put("data", data);
    }


    @RequestMapping("/add")
    public Result add(@RequestBody ShoppingCartEntity shopCard, @RequestHeader("token") String token) {
        shopCard.setCartTime(new Date());
        service.save(shopCard);
        return Result.ok("添加购物车成功！");
    }


    @RequestMapping("/update/{cartId}/{cartNum}")
    public Result update(@PathVariable("cartId") Long cartId, @PathVariable("cartNum") Long cartNum) {
        if (cartId == null) {
            return Result.error("userId不能为空!");
        }
        int i = service.updateCartnumByCartid(cartId, cartNum);
        return Result.ok();
    }


}
